﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace HRMSys.DAL
{
    public class SettingDAL
    {
        public int UpdateSetting(string name, string value)
        {
            int result = 0;
            string sql = "update T_Setting set Value=@Value where Name=@Name";
            SqlParameter nameParam = new SqlParameter("@Name", name);
            SqlParameter valueParam = new SqlParameter("@Value", value);
            result = SqlHelper.ExecuteNonQuery(sql, nameParam, valueParam);
            if (result != 1)
            {
                throw new Exception("影响行数为" + result + ". 正常情况应为1");
            }
            else
            {
                return result;
            }
        }

        public int UpdateSetting(string name, bool value)
        {
            int result = this.UpdateSetting(name, value.ToString());
            return result;
        }

        public int UpdateSetting(string name, int value)
        {
            int result = this.UpdateSetting(name, value.ToString());
            return result;
        }

        public string GetStringValue(string name)
        {
            string sql = "select value from T_Setting where Name=@Name";
            SqlParameter nameParam = new SqlParameter("@Name", name);
            DataTable table = SqlHelper.ExecuteDataTable(sql, nameParam);

            if (table.Rows.Count <= 0)
            {
                throw new Exception(name + "不存在");
            }
            else if (table.Rows.Count > 1)
            {
                throw new Exception("出现" + table.Rows.Count.ToString() +
                    "项Name=" + name + "的Setting数据");
            }
            else
            {
                string value = (string)table.Rows[0]["Value"];
                return value;
            }
        }

        public bool GetBoolValue(string name) 
        {
            string stringValue = this.GetStringValue(name);
            bool boolValue = Convert.ToBoolean(stringValue);
            return boolValue;
        }

        public int GetIntValue(string name) 
        {
            string stringValue = this.GetStringValue(name);
            int intValue = Convert.ToInt32(stringValue);
            return intValue;
        }
    }
}
